home *** CD-ROM | disk | FTP | other *** search
/ Network Supervisor's Toolkit / Network Supervisor's Toolkit.iso / tools / nwtp06 / s1_pep.pas < prev    next >
Pascal/Delphi Source File  |  1996-07-10  |  5KB  |  170 lines

  1. {$X+,V-,B-}
  2. program S1_PEP; { Listening Process / receiver / Slave }
  3.  
  4. { Testprogram for the nwPEP unit / NwTP 0.6 API. (c) 1993,1995, R.Spronk }
  5.  
  6. uses crt,nwMisc,nwIPX,nwPEP;
  7.  
  8. Var ListenECB     :Tecb;       { used to listen for packets }
  9.     ListenPepHdr  :TpepHeader;
  10.  
  11.     SendECB       :Tecb;       { used to send acknowledgements }
  12.     SendPepHdr    :TpepHeader;
  13.  
  14.     IOsocket      :word;
  15.     DataBuffer    :array[1..546] of byte;
  16.     SendDataBuffer:byte;
  17.  
  18.     PacketReceived   :Boolean;
  19.     LastTransactionID:LongInt;
  20.     BytesReceived    :Word;
  21.  
  22.     NewStack:array[1..8192] of word;  { !! used by ESR }
  23.     StackBottom:word;                 { !! used by ESR }
  24.  
  25.     lnr:word;
  26.  
  27. Procedure CheckError(err:boolean; errNbr:word);
  28. begin
  29. if err
  30.  then begin
  31.       CASE errNbr of
  32.        $0100:writeln('IPX needs to be installed.');
  33.        $0101:writeln('ERROR: Connection not established. A Timeout occured');
  34.        $0102:writeln('ERROR: The transfer is aborted; A timeout occured.');
  35.        $10FE:writeln('Error opening socket: Socket Table Is Full.');
  36.        $10FF:writeln('Error opening socket: Socket is already open.');
  37.        else writeln('Unspecified error.');
  38.       end; {case}
  39.       IPXcloseSocket(IOsocket);
  40.       halt(1);
  41.       end;
  42. end;
  43.  
  44. Function TimeOut(t1,t2:word;n:byte):boolean;
  45. { ticks t2 - ticks t1 > n seconds ? }
  46. Var lt1,lt2:LongInt;
  47. begin
  48. lt2:=t2;
  49. if t1>t2 then lt2:=lt2+$FFFF;
  50. TimeOut:=(lt2-t1)>(n*18);
  51. end;
  52.  
  53. {$F+}
  54. Procedure ListenAndAckHandler;
  55. begin
  56. lnr:=ListenPepHdr.TransactionID;
  57.  
  58. If (ListenECB.CompletionCode<>0)
  59.  or (ListenPepHdr.IPXhdr.packetType<>PEP_PACKET_TYPE)
  60.  or (ListenPepHdr.clienttype<>$EA)
  61.  or (ListenPepHdr.TransactionID<LastTransactionID) { discard dupe old packet }
  62.   then IPXlistenForPacket(ListenECB)
  63.   else begin
  64.        PacketReceived:=(ListenPepHdr.transactionID>LastTransactionID); { new packet received }
  65.  
  66.        { Acknowledge new packets and duplicates of the latest packet, }
  67.        { as the original acknowledgement may have been lost. }
  68.        BytesReceived:=swap(ListenPepHdr.IPXhdr.length)-SizeOf(TpepHeader);
  69.        LastTransactionID:=ListenPepHdr.TransactionID;
  70.  
  71.        { Setup acknowledgement ECB and PEPheader, and send it. }
  72.        if 1=1 {SendECB.InUseFlag=0}
  73.         then begin
  74.              ListenPepHdr.IPXhdr.source.socket:=swap(ListenPepHdr.IPXhdr.source.socket);
  75.              { socket is hi-lo in IPX/PEPHeaders. SetupSendECB expects lo-hi }
  76.              PEPsetupSendECB(NIL,IOsocket,ListenPepHdr.IPXhdr.source,@SendDataBuffer,0,
  77.                              SendPepHdr,SendECB);
  78.              SendPepHdr.TransactionId:=LastTransactionID;
  79.              SendPepHdr.ClientType:=$EA;
  80.              IPXsendPacket(SendECB);
  81.              end;
  82.        end;
  83. end;
  84. {$F-}
  85.  
  86. {$F+}
  87. Procedure ListenAndAckESR; assembler;
  88. asm
  89.     mov dx, seg stackbottom
  90.     mov ds, dx
  91.  
  92.     mov dx,ss  { setup of a new local stack }
  93.     mov bx,sp  { ss:sp copied to dx:bx}
  94.     mov ax,ds
  95.     mov ss,ax
  96.     mov sp,offset stackbottom
  97.     push dx    { push old ss:sp on new stack }
  98.     push bx
  99.     CALL ListenAndAckHandler
  100.     pop bx     { restore ss:sp from new stack }
  101.     pop dx
  102.     mov sp,bx
  103.     mov ss,dx
  104. end;
  105. {$F-}
  106.  
  107. Var ticks,ticks2 :word;
  108.     FileName:string;
  109.     FileSize:LongInt;
  110.  
  111. begin
  112. lnr:=0;
  113.  
  114. IpxInitialize;
  115. CheckError(nwIPX.result>0,$100);
  116.  
  117. IOSocket:=$5678;
  118. IPXopenSocket(IOsocket,SHORT_LIVED_SOCKET);
  119. CheckError(nwIPX.result>0,$1000+nwIPX.result);
  120.  
  121. { Setup of ECB and PepHeader, start listening for incoming packets. }
  122. LastTransactionID:=0;
  123. PacketReceived:=False;
  124. PEPSetupListenECB(Addr(ListenAndAckESR),IOsocket,@DataBuffer,546,
  125.                   ListenPepHdr,ListenECB);
  126. IPXListenForPacket(ListenECB);
  127. writeln('Listening for incoming packet.');
  128.  
  129. IPXGetIntervalMarker(ticks);
  130. REPEAT
  131.  IPXrelinquishControl;
  132.  IPXGetIntervalMarker(ticks2);
  133.  CheckError(TimeOut(ticks,ticks2,130),$101);{ error if a timeout occurred }
  134. UNTIL PacketReceived;
  135.  
  136. writeln('Packet received.. initiating transfer process.');
  137. writeln('Received PacketID:',LastTransactionID);
  138. writeln('len of data:',BytesReceived);
  139.  
  140. { do something with DataBuffer: the data that was just received. }
  141. { the first packet contains the filename and filesize }
  142. Move(DataBuffer[1],FileName[0],15);
  143. Move(DataBuffer[16],FileSize,4);
  144. writeln('Receiving file ',FileName,', size: ',FileSize);
  145.  
  146. REPEAT { Listen for remaining packets  }
  147.  Packetreceived:=false;
  148.  
  149.  While SendECB.InuseFlag<>0
  150.   do IPXrelinquishControl;
  151.  
  152.  IPXListenForPacket(ListenECB);
  153.  IPXGetIntervalMarker(ticks);
  154.  writeln(FileSize);
  155.  Repeat
  156.  {write(lnr);}
  157.   IPXrelinquishControl;
  158.   IPXGetIntervalMarker(ticks2);
  159.   CheckError(TimeOut(ticks,ticks2,10),$102); { error if Timeout occurred }
  160.  until PacketReceived;
  161.  writeln('Packet#:',LastTransactionID);
  162.  writeln('len of data:',BytesReceived);
  163.  FileSize:=FileSize-BytesReceived;
  164.  { do something with DataBuffer: the data that was just received. }
  165.  
  166. UNTIL (FileSize<=0); { entire file received }
  167.  
  168. writeln('Transfer complete.');
  169. IPXcloseSocket(IOsocket);
  170. end.